package com.crm.service.permission;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.crm.dao.permission.CrmDeptDAO;
import com.crm.dao.permission.MenubarDAO;
import com.crm.dao.permission.RoleDAO;
import com.crm.dao.permission.UserInfoDAO;
import com.crm.exception.ServiceException;
import com.crm.pojo.Menubar;
import com.crm.pojo.Role;
import com.crm.pojo.Userinfo;
import com.crm.vo.MenubarNode;
import com.crm.vo.MenubarVo;
import com.crm.vo.RoleNode;
import com.crm.vo.RoleVo;

@Service
@Transactional(rollbackFor = Exception.class)
public class GrantService {

	@Autowired
	private CrmDeptDAO crmDeptDAO;
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private UserInfoDAO userInfoDAO;
	@Autowired
	private MenubarDAO menubarDAO;

	/**
	 * 将Object[]转换成Role对象
	 * 
	 * @param obj
	 * @return
	 */
	private Role objToRole(Object[] obj) {
		RoleVo role = new RoleVo();
		role.setRid(((BigDecimal) obj[0]).longValue());

		role.setRname(((String) obj[1]));

		role.setChecked(((BigDecimal) obj[2]).intValue() == 1);
		return role;
	}

	/**
	 * 查找该用户的拥有角色
	 * 
	 * @param uId
	 * @return
	 */
	public List<RoleNode> getRoleForGrant(Long uId) {
		List<RoleNode> nodes = new ArrayList<RoleNode>();

		List<Object[]> obj = roleDAO.queryForUserGrantRole(uId);

		List<Role> listRole = new ArrayList<Role>();
		if (obj != null) {
			for (Object[] o : obj) {
				listRole.add(objToRole(o));
			}
		}
		RoleNode node = new RoleNode();
		node.setRname("所有角色");
		node.setList(listRole);
		nodes.add(node);

		return nodes;
	}

	/**
	 * 给指定用户赋予角色
	 * 
	 * @param uId
	 * @param rids
	 * @throws ServiceException
	 */
	public void grantRoleToUser(Long uId, List<Long> rids) throws ServiceException {
		try {
			if (rids != null) {
				Userinfo userinfo = userInfoDAO.findOne(uId);
				if (userinfo.getRoles() == null) {
					userinfo.setRoles(new ArrayList<Role>());
				}
				// 删除用户所拥有的角色
				userinfo.getRoles().clear();
				// 为用户添加角色
				for (Long rid : rids) {
					Role role = roleDAO.findOne(rid);
					userinfo.getRoles().add(role);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 将Object[]转换成Menubar对象
	 * 
	 * @param obj
	 * @return
	 */
	private Menubar objToMenubar(Object[] obj) {
		MenubarVo menubar = new MenubarVo();
		menubar.setMid(((BigDecimal) obj[0]).longValue());

		menubar.setMname(((String) obj[1]));

		menubar.setSelected(((BigDecimal) obj[2]).intValue() == 1);
		return menubar;
	}

	/**
	 * 查询该角色拥有的功能
	 * 
	 * @param id
	 * @return
	 */
	public List<MenubarNode> getMenubarToGrant(Long id) {
		List<MenubarNode> nodes = new ArrayList<MenubarNode>();

		List<Object[]> objarr = menubarDAO.queryForRoleGrantMenubar(id);

		List<Menubar> menubars = new ArrayList<Menubar>();
		if (objarr != null) {
			for (Object[] ob : objarr) {
				menubars.add(objToMenubar(ob));
			}
		}
		MenubarNode no = new MenubarNode();
		no.setMname("所有功能");
		no.setList(menubars);
		nodes.add(no);

		return nodes;
	}

	/**
	 * 给角色授权
	 * 
	 * @param id
	 * @param mids
	 * @throws ServiceException
	 */
	public void grantMenubarToRole(Long id, List<Long> mids) throws ServiceException {
		try {
			if (mids != null) {
				Role role = roleDAO.findOne(id);
				if (role.getMenubars() == null) {
					role.setMenubars(new ArrayList<Menubar>());
				}
				// 删除角色所拥有的功能
				role.getMenubars().clear();
				// 为角色添加功能
				for (Long mid : mids) {
					Menubar menubar = menubarDAO.findOne(mid);
					role.getMenubars().add(menubar);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 登录
	 * 
	 * @param name
	 * @param pwd
	 * @return
	 */
	public Userinfo toLogin(String name, String pwd) {
		Userinfo userinfo = userInfoDAO.findByUnameAndUpassword(name, pwd);
		if (userinfo != null) {
			return userinfo;
		}else{
			return null;
		}
		
	}

	/**
	 * 根据用户uId查询所有功能集合
	 * 
	 * @param uId
	 * @return
	 */
	public List<Long> queryAllMidsByuId(Long uId) {
		return userInfoDAO.queryMidsByuId(uId);
	}
	
	
	public void updateOUserself(Userinfo userinfo){
		userInfoDAO.save(userinfo);
	}
	
	public List<Userinfo> queryAllUsers(){
		return userInfoDAO.queryAllUser();
	}
	public Userinfo queryOneUsers(String uname){
		return userInfoDAO.queryNameUser(uname);
	}
	
	public Userinfo queryOneUserId(Long uId){
		return userInfoDAO.queryIdUser(uId);
	} 
}
